package com.hao;

import com.hao.config.MyRabbitMQConfig;
import com.hao.config.RabbitMQConfig;
import com.hao.config.ReturnRabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@SpringBootTest
@RunWith(SpringRunner.class)
public class test {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void testSend(){
        rabbitTemplate.convertAndSend(MyRabbitMQConfig.EXCHANGE_NAME,"boot.cc","boot my hello1~~");
    }
    @Test
    public void testConfirm() throws InterruptedException {
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("方法被执行了！");
                if (ack){
                    System.out.println("接收消息成功"+cause);
                }else {
                    System.out.println("接收消息失败"+cause);
                }
            }
        });

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"confirm","message confirm~~");
        Thread.sleep(2000);
    }
    @Test
    public void testReturn() throws InterruptedException {

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("执行了 ...");
            }
        });
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"confirm","message return~~");
        Thread.sleep(2000);
    }
}
